import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import Antd from 'ant-design-vue';
import { notification } from 'ant-design-vue';
import 'ant-design-vue/dist/reset.css';
import * as Icons from '@ant-design/icons-vue'
import axios from 'axios';


const app = createApp(App)
app.use(Antd).use(store).use(router).mount('#app')

const icons = Icons
for(const i in icons){
    app.component(i,icons[i])
}

/**
 * axios拦截器
 */
// 请求的拦截器
axios.interceptors.request
    .use( 
        // 成功请求
        config => {
            console.log('请求参数：', config);
            const token = store.state.member.token
            if(token){
                config.headers.token = token
            }
            return config;
        }, 
        // 失败请求
        error => {
            return Promise.reject(error);
        }
    );


// 响应的拦截器
axios.interceptors.response
    .use( 
        // 成功响应
        response => {
            console.log('返回结果：', response);
            return response;
        },
        // 失败响应
        error => {
            console.log('返回错误：', error);
            const response = error.response;
            const status = response.status;
            if (status === 401) {
                // 判断状态码是401 跳转到登录页
                console.log("未登录或登录超时，跳到登录页");
                notification.error({ description: "未登录或登录超时" });
                store.commit("setMember", {});
                router.push('/login');
            }
            return Promise.reject(error);
        }
    );
    
// 设置axios的统一请求前缀 
axios.defaults.baseURL = process.env.VUE_APP_SERVER;
console.log('环境：', process.env.NODE_ENV);
console.log('服务端：', process.env.VUE_APP_SERVER);